package com.moke.hit
{
	import flash.display.Sprite;
	
	/**
	 * 网格划分工具
	 * @author moke
	 */ 
	public class HitGridUtil
	{
		public static const DEFAULT_GRID_SIZE:uint = 10;
		
		/**
		 * 对容器划分格子
		 *
		 */
		public static function makeGrid(container:Sprite, gridSize:uint = DEFAULT_GRID_SIZE):Vector.<GridInfo>
		{
			var infos:Vector.<GridInfo> = new Vector.<GridInfo>();
			var wCounter:uint = Math.ceil(container.width / gridSize);
			var hCounter:uint = Math.ceil(container.height / gridSize);
			
			for (var i:uint = 0; i < wCounter; i++)
			{
				for (var j:uint = 0; j < hCounter; j++)
				{
					var grid:GridInfo = new GridInfo(i, j);
					infos.push(grid);
				}
			}
			
			return infos;
		}
		
		/** 将碰撞检测对象赋值到对应的方格 */
		public static function assignHitObject(hitObject:IHitObject, infos:Vector.<GridInfo>, gridSize:uint = DEFAULT_GRID_SIZE):void
		{
			var iPos:uint = Math.floor(hitObject.x / gridSize);
			var jPos:uint = Math.floor(hitObject.y / gridSize);
			
			for each (var info:GridInfo in infos)
			{
				if (info.rowIndex == iPos && info.columnIndex == jPos)
				{
					info.addHitObject(hitObject);
					break;
				}
			}
		}
	}
}